//
//  PaperReportCell.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/3/14.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import SnapKit

class PaperReportCell: UICollectionViewCell {
    
    var label: UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    private func setup() {
        backgroundColor = UIColor.white
        label = UILabel().then({ (label) in
            label.font = UIFont.systemFont(ofSize: 14)
            label.layer.cornerRadius = 30 / 2
            label.clipsToBounds = true
            label.textAlignment = .center
            label.layer.borderWidth = 0.5
            
            contentView.addSubview(label)
            label.snp.makeConstraints({ (maker) in
                maker.edges.equalTo(UIEdgeInsets.zero)
            })
        })
    }
    
    ///题卡页
    public func set(_ index: Int?, userAnswer: String?) {
        label.text = "\(index ?? 0)"
        
        if String.lsf_hasValue(userAnswer) {
            label.textColor = UIColor.white
            label.backgroundColor = UIColor.lsf_color16(0x76A6FF)
            label.layer.borderColor = UIColor.clear.cgColor
        }
        else {
            label.textColor = UIColor.lsf_color16(0x9B9B9B)
            label.backgroundColor = UIColor.white
            label.layer.borderColor = UIColor.lsf_color16(0x9B9B9B).cgColor
        }
    }
    
    ///答题报告页
    public func set(_ index: Int?, result: PaperItemAnswerResult?) {
        label.text = "\(index ?? 0)"
        
        let result = result ?? .none
        var borderColor = UIColor.clear
        var backColor = UIColor.white
        var textColor = UIColor.white
        switch result {
            //未答题，灰色
        case .none:
            backColor = UIColor.white
            borderColor = UIColor.lsf_color16(0x9B9B9B)
            textColor = borderColor
            //正确，绿色
        case .correct:
            backColor = UIColor.lsf_color16(0x50E3AC)
            
            //打错了， 红色
        case .error:
            backColor = UIColor.lsf_color16(0xF5586A)
            ///主观题自评分
        case .selfReported:
            backColor = UIColor.lsf_color16(0xFF973E)
        }
        
        label.textColor = textColor
        label.backgroundColor = backColor
        label.layer.borderColor = borderColor.cgColor
    }
}
